package com.xiaotu.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.RandomUtil;
import com.xiaotu.constant.BattleModeEnum;
import com.xiaotu.constant.CampType;
import com.xiaotu.mapper.PlayerMapper;
import com.xiaotu.mapper.RoomMapper;
import com.xiaotu.model.PlayerModel;
import com.xiaotu.model.RoomModel;
import com.xiaotu.service.RoomService;
import com.xiaotu.vo.BattleModeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author 张晓
 * @create 2021-10-14 20:31
 */
@Service
public class RoomServiceImpl implements RoomService {

    public static final List<String> BlackList = new ArrayList<>(16);

    public static final List<String> RedList = new ArrayList<>(16);

    public static final Map<Integer, Map<String, Object>> redEnvelopes = new ConcurrentHashMap<>();

    static {
        BlackList.add("黑车1");
        BlackList.add("黑车2");
        BlackList.add("黑马1");
        BlackList.add("黑马2");
        BlackList.add("黑相1");
        BlackList.add("黑相2");
        BlackList.add("黑士1");
        BlackList.add("黑士2");
        BlackList.add("将");
        BlackList.add("黑炮1");
        BlackList.add("黑炮2");
        BlackList.add("黑卒1");
        BlackList.add("黑卒2");
        BlackList.add("黑卒3");
        BlackList.add("黑卒4");
        BlackList.add("黑卒5");

        RedList.add("红车1");
        RedList.add("红车2");
        RedList.add("红马1");
        RedList.add("红马2");
        RedList.add("红相1");
        RedList.add("红相2");
        RedList.add("红士1");
        RedList.add("红士2");
        RedList.add("帅");
        RedList.add("红炮1");
        RedList.add("红炮2");
        RedList.add("红兵1");
        RedList.add("红兵2");
        RedList.add("红兵3");
        RedList.add("红兵4");
        RedList.add("红兵5");
    }

    @Autowired
    private RoomMapper roomMapper;

    @Autowired
    private PlayerMapper playerMapper;

    @Override
    @Transactional
    public BattleModeVo getBattleMode(Integer playerId) {
        BattleModeVo battleMode = new BattleModeVo();
        RoomModel roomModel = roomMapper.getRoom();
        if (ObjectUtil.isNotNull(roomModel)) {
            battleMode.setCamp(CampType.Black.getVal());
        } else {
            roomModel = new RoomModel(null, 1);
            roomMapper.insert(roomModel);
            battleMode.setCamp(CampType.Red.getVal());
        }
        PlayerModel playerModel = playerMapper.getPreparingPlayer(playerId);
        if (ObjectUtil.isNotNull(playerModel)) {
            battleMode.setMode(BattleModeEnum.PLAYER.getVal());
        } else {
            battleMode.setMode(BattleModeEnum.MACHINE.getVal());
        }
        battleMode.setRoomId(roomModel.getRoomId());
        return battleMode;
    }

    @Transactional
    @Override
    public void closeRoom(Integer roomId) {
        RoomModel roomModel = new RoomModel();
        roomModel.setRoomId(roomId);
        roomModel.setAvailableStatus(0);
        roomMapper.updateByPrimaryKeySelective(roomModel);
    }

    @Override
    public Object getRoomRedEnvelopes(Integer roomId) {
        Map<String, Object> res = null;
        if (redEnvelopes.containsKey(roomId)) {
            res = redEnvelopes.get(roomId);
            redEnvelopes.remove(roomId);
        } else {
            res = new HashMap<>();
            res.put("blackList", RandomUtil.randomEleList(BlackList, 3));
            res.put("redList", RandomUtil.randomEleList(RedList, 3));
            redEnvelopes.put(roomId, res);
        }
        return res;
    }

    @Transactional
    @Override
    public void activeRoom(Integer roomId) {
        Example example = new Example(RoomModel.class);
        example.createCriteria().andEqualTo("roomId", roomId)
                .andEqualTo("availableStatus", 0);
        RoomModel roomModel = new RoomModel();
        roomModel.setAvailableStatus(1);
        roomMapper.updateByExampleSelective(roomModel, example);
    }

}
